package cn.edu.fzu.homemaking.sso.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.exception.ServiceException;
import cn.edu.fzu.homemaking.sso.domain.entity.CompanyUser;
import cn.edu.fzu.homemaking.sso.domain.entity.SysUser;
import cn.edu.fzu.homemaking.sso.domain.entity.User;
import cn.edu.fzu.homemaking.sso.domain.enums.StatusEnum;
import cn.edu.fzu.homemaking.sso.mapper.RoleMapper;
import cn.edu.fzu.homemaking.sso.service.RoleService;
import com.github.pagehelper.Page;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.edu.fzu.homemaking.sso.domain.entity.Role;
import com.github.pagehelper.PageHelper;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public PageVO<Role> list(String name, StatusEnum status, Integer pageNum, Integer pageSize) {
        try (Page<Role> page = PageHelper.startPage(pageNum, pageSize)) {
            roleMapper.list(name, status);
            return new PageVO<>(page.getTotal(), page.getResult());
        }
    }


    @Override
    public void add(String name, String description, User currentUser) {
        Long companyId = null;
        if (currentUser instanceof CompanyUser) {
            CompanyUser companyUser = (CompanyUser) currentUser;
            companyId = companyUser.getCompany().getId();
        }
        roleMapper.insert(name, description, StatusEnum.BAN, companyId);
    }


    @Override
    public void update(Long id, String description, StatusEnum status, User currentUser) {
        if (currentUser instanceof CompanyUser) {
            CompanyUser companyUser = (CompanyUser) currentUser;
            Role role = roleMapper.getById(id);
            if (!Objects.equals(role.getCompanyId(), companyUser.getCompany().getId())) {
                throw new ServiceException("非法操作,只能修改公司拥有的角色");
            }
        }
        roleMapper.update(id, description, status);
    }


    @Override
    public void delete(Long id, User currentUser) {
        if (currentUser instanceof CompanyUser) {
            CompanyUser companyUser = (CompanyUser) currentUser;
            Role role = roleMapper.getById(id);
            if (!Objects.equals(role.getCompanyId(), companyUser.getCompany().getId())) {
                throw new ServiceException("非法操作,只能删除公司拥有的角色");
            }
        }
        roleMapper.delete(id);
    }


    @Override
    @Transactional
    public void grant(Long userId, List<Long> roleIds, User currentUser) {
        if (currentUser instanceof CompanyUser) {
            CompanyUser companyUser = (CompanyUser) currentUser;
            List<Long> loadIds = listRoleIdByUserId(companyUser.getId());
            if (CollectionUtils.isEmpty(loadIds) || !new HashSet<>(loadIds).containsAll(roleIds)) {
                throw new ServiceException("非法操作,授权角色包含非本人拥有的角色");
            }
        }
        //先删后增
        roleMapper.deleteUserRoleByUserId(userId);
        roleMapper.addUserRole(userId, roleIds);
    }


    @Override
    public List<Long> listRoleIdByUserId(Long userId) {
        return roleMapper.listRoleIdByUserId(userId);
    }


    @Override
    public List<Long> listUserIdByRoleId(Long roleId) {
        return roleMapper.listUserIdByRoleId(roleId);
    }


    @Override
    public List<Role> listCurrentUserRole(User currentUser) {
        if (currentUser instanceof SysUser) {
            //系统用户拥有所有权限
            return roleMapper.list(null, StatusEnum.NORMAL);
        }
        return roleMapper.listRoleByUserId(currentUser.getId());
    }
}
